use rand;

use usrtrust::{UTime,SysTime,SysConvertor,Clocker,CpuClock,UClock,UClockExchange};
use usrtrust::{ULogs,Environment,SysBlocks};
use std::path::{Path};
use std::io::{Write,Result};
use std::thread;
use std::time::Duration;

fn main() -> Result<()> {
    let e = Environment::from_dir(Path::new("/tmp/usrtos"))?;
    let fsys = e.find("system_block")?;
    println!("{}",fsys);
    let mut uclock = UClock::<CpuClock>::new();
    println!("clock {:?}",uclock);
    let mut sys = SysBlocks::new(fsys)?;
    let mut clkex = sys.get_items::<UClockExchange>("clock_a")?;
    uclock.to_data(&mut clkex);
    loop {
        for i in 0..10 {
            let mut c = CpuClock::default();
            let (cpu,sys) = (c.now(),SysTime::now());
            println!("cpu: {:?} diff {:?}",cpu,cpu.diff(&uclock.data.from_sys(&sys)));
            println!("sys: {:?} diff {:?}",sys,sys.diff(&uclock.data.to_sys(&cpu)));
            thread::sleep(Duration::from_millis(1000));
        }
        uclock = UClock::<CpuClock>::from_data(&mut clkex);
    }
    Ok(())
}